﻿using MyAppAPI.Models.Hotel;
using MyAppAPI.ParamModel;
using MyAppAPI.Repositories.interfaceRepo;
using MyAppAPI.Services.interfaceService;

namespace MyAppAPI.Services
{
    public class HotelService:IHotelService
    {
        private IHotelRepository hotelRepository;
        public  HotelService(IHotelRepository _hotelRepository) 
        {
            hotelRepository= _hotelRepository;
        }
        public async Task<UserData> Login(string loginId, string loginPwd)
        {
            return await hotelRepository.Login(loginId, loginPwd);
        }
        public async Task<bool> AddRoleName(string roleName)
        {
            return await hotelRepository.AddRoleName(roleName);
        }

        public async Task<List<RoleData>> GetAllRoleType()
        {
            return await hotelRepository.GetAllRoleType();
        }
        public async Task<bool> DeleteRoleName(int roleId)
        {
            return await hotelRepository.DeleteRoleName(roleId);
        }
        public async Task<RoleData> GetRoleByRoleId(int roleId)
        {
            return await hotelRepository.GetRoleByRoleId(roleId);
        }
        public bool UpdateRoleByRoleId(RoleData roleData)
        {
            return hotelRepository.UpdateRoleByRoleId(roleData);
        }
        public List<UserData> GetAllUser(ref int count, int pageIndex, int pageSize)
        {
            return hotelRepository.GetAllUser(ref count,pageIndex,pageSize);
        }
        public async Task<UserData> GetUser(string loginId)
        {
            return await hotelRepository.GetUser(loginId);
        }

        public List<UserData> GetAllUserByRoleId(int roleId, ref int count, int pageIndex, int pageSize)
        {
            return hotelRepository.GetAllUserByRoleId(roleId,ref count,pageIndex,pageSize);
        }
        public async Task<bool> AddUser(UserData userData)
        {
            return await hotelRepository.AddUser(userData);
        }
        public bool UpdateUser(UserData userData)
        {
            return hotelRepository.UpdateUser(userData);
        }
        public async Task<bool> DeleteUser(int id)
        {
            return await hotelRepository.DeleteUser(id);
        }
        public async Task<bool> ResetPwd(ResetPwdParams resetPwdParams)
        {
            return await hotelRepository.ResetPwd(resetPwdParams);
        }
        public async Task<List<RoomTypeData>> GetAllRoomType()
        {
            return await hotelRepository.GetAllRoomType();
        }
        public async Task<bool> AddRoomType(RoomTypeData roomTypeData)
        {
            return await hotelRepository.AddRoomType(roomTypeData);
        }
        public async Task<RoomTypeData> GetRoomTypeByRoomTypeId(int roomTypeId)
        {
            return await hotelRepository.GetRoomTypeByRoomTypeId(roomTypeId);
        }
        public bool UpdateRoomByRoomTypeId(RoomTypeData roomTypeData)
        {
            return hotelRepository.UpdateRoomByRoomTypeId(roomTypeData);
        }
        public async Task<bool> DeleteRoomType(int roomTypeId)
        {
            return await hotelRepository.DeleteRoomType(roomTypeId);
        }
        public List<RoomDetailData> GetAllRoomDetails(ref int count, int roomTypeId,int roomStateId, int pageIndex, int pageSize,int guestId)
        {
            //获取alldata
            List<RoomDetailData> roomDetailDatas = new();
            var roomDatas=hotelRepository.GetAllRoom(roomTypeId,roomStateId,guestId);
            
            var roomStates = hotelRepository.GetAllRoomState(0);
            var roomTypes = hotelRepository.GetAllRoomType().Result;
            roomDatas.ForEach(obj =>
            {
                RoomDetailData roomDetailData = new();
                roomDetailData.roomId=obj.roomId;
                roomDetailData.roomTypeId = obj.roomTypeId;
                roomDetailData.roomStateId = obj.roomStateId;
                roomDetailData.description = obj.description;
                roomDetailData.roomType= roomTypes.Where(x => x.RoomTypeId==obj.roomTypeId).First();
                roomDetailData.roomState=roomStates.Where(x => x.roomStateId==obj.roomStateId).First();
                roomDetailDatas.Add(roomDetailData);
            });
            count = roomDetailDatas.Count;
            return roomDetailDatas.Skip((pageIndex-1)*pageSize).Take(count>pageIndex*pageSize?pageSize:(count-(pageIndex-1)*pageSize)).ToList();
        }
        public List<RoomStateData> GetAllRoomState(int flag)
        {
            return hotelRepository.GetAllRoomState(flag);
        }
        public async Task<bool> AddRoom(RoomData roomData)
        {
            return await hotelRepository.AddRoom(roomData);
        }
        public async Task<RoomData> GetRoomByRoomId(int roomId)
        {
            return await hotelRepository.GetRoomByRoomId(roomId);
        }
        public bool UpdateRoomData(RoomData roomData)
        {
            return hotelRepository.UpdateRoomData(roomData);
        }
        public bool DeleteRoomData(int roomId)
        {
            return hotelRepository.DeleteRoomData(roomId);
        }
        public List<GuestDetailData> GetAllGuestRecord(ref int count, string guestName, int resideStateId, int pageIndex, int pageSize)
        {
            List<GuestDetailData> guestDetailDatas = new();
            var guestDetailData = hotelRepository.GetAllGuest(guestName,resideStateId);
            List<RoomDetailData> roomDetailDatas = new();
            var roomDatas = hotelRepository.GetAllRoom(0, 0,0);

            var roomStates = hotelRepository.GetAllRoomState(0);
            var roomTypes = hotelRepository.GetAllRoomType().Result;
            roomDatas.ForEach(obj =>
            {
                RoomDetailData roomDetailData = new();
                roomDetailData.roomId = obj.roomId;
                roomDetailData.roomTypeId = obj.roomTypeId;
                roomDetailData.roomStateId = obj.roomStateId;
                roomDetailData.description = obj.description;
                roomDetailData.roomType = roomTypes.Where(x => x.RoomTypeId == obj.roomTypeId).First();
                roomDetailData.roomState = roomStates.Where(x => x.roomStateId == obj.roomStateId).First();
                roomDetailDatas.Add(roomDetailData);
            });
            var resideStateData = hotelRepository.GetAllResideStateData();
            guestDetailData.ForEach(obj => {
                GuestDetailData guestDetail = new();
                guestDetail.guestId=obj.guestId;
                guestDetail.guestName = obj.guestName;
                guestDetail.resideStateId = obj.resideStateId;
                guestDetail.identityId = obj.identityId;
                guestDetail.phone= obj.phone;
                guestDetail.guestNum = obj.guestNum;
                guestDetail.deposit=obj.deposit;
                guestDetail.resideDate=obj.resideDate;
                guestDetail.leaveDate=obj.leaveDate;
                guestDetail.totalMoney=obj.totalMoney;
                guestDetail.roomId=obj.roomId;
                guestDetail.resideState=resideStateData.Where(o=>o.resideStateId==obj.resideStateId).First();
                guestDetail.room = roomDetailDatas.Where(o => o.roomId == obj.roomId).First();
                guestDetailDatas.Add(guestDetail);
            });
            count = guestDetailDatas.Count;
            return guestDetailDatas.Skip((pageIndex - 1) * pageSize).Take(count > pageIndex * pageSize ? pageSize : (count - (pageIndex - 1) * pageSize)).ToList();
        }
        public List<ResideState> GetAllResideStateData()
        {
            return hotelRepository.GetAllResideStateData();
        }
        public ResideState GetResideSatetByStateId(int resideStateId)
        {
            return hotelRepository.GetResideSatetByStateId(resideStateId);
        }
        public bool AddGuestRecord(GuestData guestData)
        {
            return hotelRepository.AddGuestRecord(guestData);
        }
        public GuestReturnOneData GetGuestDataByGuestId(int guestId)
        {
            GuestReturnOneData guestReturnOneData = new();
            var guestData=hotelRepository.GetGuestDataByGuestId(guestId);
            var roomData=hotelRepository.GetRoomByRoomId(guestData.roomId).Result;
            var roomTypeData=hotelRepository.GetRoomTypeByRoomTypeId(roomData.roomTypeId).Result;
            guestReturnOneData.room = roomTypeData;
            guestReturnOneData.guestId=guestId;
            guestReturnOneData.guestNum=guestData.guestNum;
            guestReturnOneData.guestName=guestData.guestName;
            guestReturnOneData.identityId=guestData.identityId;
            guestReturnOneData.phone=guestData.phone;
            guestReturnOneData.roomId=roomData.roomId;
            guestReturnOneData.deposit=guestData.deposit;
            guestReturnOneData.resideDate=guestData.resideDate;
            guestReturnOneData.leaveDate=guestData.leaveDate;
            guestReturnOneData.totalMoney=guestData.totalMoney;
            guestReturnOneData.resideStateId=guestData.resideStateId;
            return guestReturnOneData;
        }
        public bool UpdateGuestData(GuestData guestData)
        {
            return hotelRepository.UpdateGuestData(guestData);
        }
        public bool DeleteGuestDataByGuestId(int guestId)
        {
            return hotelRepository.DeleteGuestDataByGuestId(guestId);
        }
        public GuestData Checkout(int guestId)
        {
            return hotelRepository.Checkout(guestId);
        }
        public List<RoomTypeTotalData> GetRoomTypeTotalMoney()
        {
            return hotelRepository.GetRoomTypeTotalMoney();
        }
    }
}
